<?php

namespace App\Resource\Admin;

use App\Model\Article;
use Hyperf\Resource\Json\JsonResource;

/**
 * @mixin \App\Model\User
 */
class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array
     */
    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'avatar' => $this->avatar,
            'role' => $this->role,
            'status' => $this->status,
            'points' => $this->points,
            'last_login_at' => $this->last_login_at?->format('Y-m-d H:i:s'),
            'last_login_ip' => $this->last_login_ip,
            'email_verified_at' => $this->email_verified_at?->format('Y-m-d H:i:s'),
            'created_at' => $this->created_at?->format('Y-m-d H:i:s'),

            'articles' => $this->whenLoaded('articles', fn () => $this->articles->map(fn (Article $article) => [
                'id' => $article->id,
                'title' => $article->title,
                'cover_image' => $article->cover_image,
                'status' => $article->status,
                'view_count' => $article->view_count,
                'like_count' => $article->like_count,
                'comment_count' => $article->comment_count,
                'favorite_count' => $article->favorite_count,
                'reading_time' => $article->reading_time,
                'published_at' => $article->published_at?->format('Y-m-d H:i:s'),
                'created_at' => $article->created_at?->format('Y-m-d H:i:s'),
            ]))
        ];
    }
}
